import { createSlice } from '@reduxjs/toolkit'
const tabSlice = createSlice({
    name: 'tab',
    initialState: {
        isCollapsed: false,
        tabList: [
            {
                path:'/home',
                name: 'home',
                label: '首页'
            }
        ],
        currentMenu: {},
        selectMenuKey: '/home'
    },
    reducers: {
      collapseMenu: state => {
          state.isCollapsed = !state.isCollapsed
      },
       selectMenuList: (state, { payload: val}) => {
           state.selectMenuKey = val.path
           if(val.name !== 'home') {
               state.currentMenu = val
               if(!state.tabList.find(item => item.name === val.name)) {
                   state.tabList.push(val)
               }
           } else if(val.name === 'home' && state.tabList.length === 1)  {
               state.currentMenu = {}
           }
       },
        closeTab: (state, { payload: val }) =>{
          let index = state.tabList.findIndex((item => item.name === val.name))
          state.tabList.splice(index,1)
        },
        setCurrentMenu: (state, { payload: val }) => {
            state.selectMenuKey = val.path
          if(val.name === 'home' ) {
              state.currentMenu = {}
          } else  {
              state.currentMenu = val
          }
        }
    }
})

export const { collapseMenu,selectMenuList ,closeTab,setCurrentMenu} = tabSlice.actions
export default tabSlice.reducer
